主题
断开连接 - TcpClientDisconnect
函数简介
断开TCP客户端连接。
接口名称
TcpClientDisconnectDLL调用
c
int32_t TcpClientDisconnect(int64_t instance, int64_t client_handle);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| client_handle | 长整数型 | 客户端句柄。 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long client = ola.TcpClientCreate(nullptr, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000);
// ...业务逻辑...
int ret = ola.TcpClientDisconnect(client);
ola.TcpClientDestroy(client);
}csharp
using OLAPlug;
var ola = new OLAPlugServer();
long client = ola.TcpClientCreate(null, 0, 1);
if (client != 0)
{
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000);
int ret = ola.TcpClientDisconnect(client);
ola.TcpClientDestroy(client);
}python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
client = ola.TcpClientCreate(None, 0, 1)
if client:
# enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000)
ret = ola.TcpClientDisconnect(client)
ola.TcpClientDestroy(client)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
long client = ola.TcpClientCreate(null, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000);
int ret = ola.TcpClientDisconnect(client);
ola.TcpClientDestroy(client);
}go
import "github.com/ola/olaplug/olaplug"
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
long client = ola.TcpClientCreate(nil, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000)
ret := ola.TcpClientDisconnect(client)
ola.TcpClientDestroy(client);
}rust
use olaplug::OLAPlugServer;
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
long client = ola.TcpClientCreate(None, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.tcp_client_connect(&client, "127.0.0.1", 9000);
let ret = ola.tcp_client_disconnect(&client);
ola.TcpClientDestroy(client);
}cpp
var ola = com("OlaPlug.OlaSoft")
long client = ola.TcpClientCreate(0, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000)
var ret = ola.TcpClientDisconnect(client)
ola.TcpClientDestroy(client);
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
client = ola.TcpClientCreate(0, 0, 1)
If client <> 0 Then
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000)
ret = ola.TcpClientDisconnect(client)
ola.TcpClientDestroy(client)
End Iftext
.局部变量 ola, OLAPlug
ola.创建 ()
client = ola.TcpClientCreate (0, 0, 1)
.如果真 (client ≠ 0)
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect (client, "127.0.0.1", 9000)
ret = ola.TcpClientDisconnect (client)
ola.TcpClientDestroy (client)
.如果真结束aardio
import OLAPlugServer;
var ola = OLAPlugServer();
long client = ola.TcpClientCreate(null, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000);
var ret = ola.TcpClientDisconnect(client);
ola.TcpClientDestroy(client);
}text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
long client = ola.TcpClientCreate(0, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000)
整数 ret = ola.TcpClientDisconnect(client)
ola.TcpClientDestroy(client);
}cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long client = ola.TcpClientCreate(nullptr, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000);
int ret = ola.TcpClientDisconnect(client);
ola.TcpClientDestroy(client);
}原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
long client = TcpClientCreate(instance, 0, instance, 1);
if (client != 0) {
TcpClientConnect(instance, client, "127.0.0.1", 9000);
TcpClientDisconnect(instance, client);
TcpClientDestroy(instance, client);
}csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long TcpClientCreate(long ola, long callback, long user_data, int enable_packet_protocol);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int TcpClientDestroy(long ola, long client_handle);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int TcpClientConnect(long ola, long client_handle, string host, int port);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int TcpClientDisconnect(long ola, long client_handle);
long instance = CreateCOLAPlugInterFace();
long client = TcpClientCreate(instance, 0, instance, 1);
if (client != 0) {
TcpClientConnect(instance, client, "127.0.0.1", 9000);
TcpClientDisconnect(instance, client);
TcpClientDestroy(instance, client);
}python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
client = ola.TcpClientCreate(instance, 0, instance, 1)
if client:
ola.TcpClientConnect(instance, client, "127.0.0.1", 9000);
ola.TcpClientDisconnect(instance, client);
ola.TcpClientDestroy(instance, client)返回值
| 返回值 | 说明 |
|---|---|
| (返回值) | 整数型,1 成功,0 失败。 |
注意事项
| 项目 | 说明 |
|---|---|
| 断开后会触发回调(event_type=3) | 断开后会触发回调(event_type=3)。 |
| 断开后可重连 | 断开后可以重新调用 TcpClientConnect 连接。 |
| 如果未连接 | 如果未连接,调用会失败。 |
| 断开是异步操作 | 断开是异步操作,可能不会立即完成。 |
